package cn.wolfcode.wolf2w.business.service.impl;

import cn.wolfcode.wolf2w.business.api.domain.Strategy;
import cn.wolfcode.wolf2w.business.api.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.business.mapper.StrategyCatalogMapper;
import cn.wolfcode.wolf2w.business.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.business.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.business.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.business.vo.CatalogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.org.apache.xml.internal.resolver.Catalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 攻略分类Service业务层处理
 * 
 * @author fang
 * @date 2024-09-27
 */
@Service
@Transactional
public class StrategyCatalogServiceImpl extends ServiceImpl<StrategyCatalogMapper, StrategyCatalog> implements IStrategyCatalogService {
    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyCatalog> queryPage(StrategyCatalogQuery qo) {
        IPage<StrategyCatalog> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    //查询数据库
    @Override
    public List<StrategyCatalog> queryCatalogList(Long destId) {
        //只有返回的是当前类数据类型才可以使用lambda
        List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestId, destId)
                .orderByAsc(StrategyCatalog::getSeq)
                .list();
        for (StrategyCatalog strategyCatalog : list) {
            LambdaQueryWrapper<Strategy> qw = new LambdaQueryWrapper<>();
            qw.eq(Strategy::getCatalogId, strategyCatalog.getId());
            List<Strategy> strategies = strategyMapper.selectList(qw);
            strategyCatalog.setStrategies(strategies);
        }
        return list;
    }

    @Override
    public List<CatalogVO> queryGroupList() {
        List<StrategyCatalog> distinct_dest_name = query().select("distinct dest_name").list();
        List<CatalogVO> vos = new ArrayList<>();
        for (StrategyCatalog strategyCatalog : distinct_dest_name) {
            String destName = strategyCatalog.getDestName();
            List<StrategyCatalog> list = lambdaQuery().eq(StrategyCatalog::getDestName, destName).list();
            CatalogVO catalogVO = new CatalogVO(destName,list);
            vos.add(catalogVO);
        }
        return vos;
    }

}
